 /*   
 * Project: OSMP
 * FileName: SceneController.java
 * version: V1.0
 */
package com.cnpc.iot.scene.controller;

 import com.alibaba.fastjson.JSONObject;
 import com.cnpc.framework.annotation.RefreshCSRFToken;
 import com.cnpc.framework.base.entity.Org;
 import com.cnpc.framework.base.entity.SysFile;
 import com.cnpc.framework.base.pojo.Result;
 import com.cnpc.framework.base.service.BaseService;
 import com.cnpc.framework.base.service.OrgService;
 import com.cnpc.iot.scene.entity.Device;
 import com.cnpc.iot.scene.entity.MapSetting;
 import com.cnpc.iot.scene.service.DeviceService;
 import com.cnpc.iot.scene.vo.SensorVo;

 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;

 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;

 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;

/**
 * Description:场景设置
 * @author: wangkaiping
 * @date: 2018年2月10日 下午4:46:37上午10:51:30
 */
@Controller
@RequestMapping("/scene")
public class SceneController {
	
	@Resource
    private BaseService baseService;
	
	@Resource
    private OrgService orgService;
	
	@Resource
	private DeviceService deviceService;
	
    @RequestMapping(value="/addmap",method = RequestMethod.GET)
    public String addmap(String id,HttpServletRequest request){
		request.setAttribute("id", id);
        return "iot/scene/addmap";
    }
	
	@RefreshCSRFToken
    @RequestMapping(value="/addimg",method = RequestMethod.GET)
    public String addimg(String id,HttpServletRequest request){
        request.setAttribute("id", id);
        return "iot/scene/addimg";
    }
    
    @RefreshCSRFToken
    @RequestMapping(value="/showImage/{id}",method = RequestMethod.GET)
    public String showimg(@PathVariable("id") String id,HttpServletRequest request){
    	Org org = baseService.get(Org.class, id);
        request.setAttribute("org",org);
        return "iot/scene/showimg";
    }
    
    @RequestMapping(value="/saveImage/{id}/{fileIds}",method = RequestMethod.POST)
    @ResponseBody
    public Result saveImage(@PathVariable("id") String id, @PathVariable("fileIds") String fileIds, HttpServletRequest request){
    	Org org = baseService.get(Org.class, id);
    	SysFile file = baseService.get(SysFile.class, fileIds);
    	org.setImages(file.getFilePath().replaceAll("\\\\", "/"));
        baseService.update(org);
        return new Result(true);
    }
    
    @RequestMapping(value="/saveMap/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result saveMap(@PathVariable("orgId") String orgId, String marker, String mapCenter, String mapZoom, HttpServletRequest request){
		List<Device> devices = baseService.find("from Device where orgid='" + orgId + "'");
		if(null != devices && !devices.isEmpty()){
			Result re = new Result(false);
			re.setMessage("请先删除关联的设备信息后再添加");
			return re;
		}
		List<MapSetting> mpaSettings = baseService.find("from MapSetting where orgid='"+orgId+"'");
		if(null != mpaSettings && !mpaSettings.isEmpty()){
			Result re = new Result(false);
			re.setMessage("请先删除关联的定位信息后再添加");
			return re;
		}
		Org org = baseService.get(Org.class, orgId);
    	Map<String,Float> mc = JSONObject.parseObject(mapCenter, Map.class);
    	MapSetting temp = new MapSetting();
    	temp.setOrgid(orgId);
    	temp.setLat(String.valueOf(mc.get("lat")));
    	temp.setLng(String.valueOf(mc.get("lng")));
    	temp.setZoom(String.valueOf(mapZoom));
    	baseService.save(temp);
    	List<Map> markers = JSONObject.parseArray(marker, Map.class); 
    	if(null != markers && !markers.isEmpty()) {
    		for(Map m : markers) {
    			String type = (String) m.get("type");
    			String lng = String.valueOf(m.get("lng"));
    			String lat = String.valueOf(m.get("lat"));
    			Device device = new Device();
    			device.setOrgid(orgId);
    			device.setLevelCode(org.getLevelCode());
    			device.setType(type);
    			device.setPx(lng);
    			device.setPy(lat);
    			baseService.save(device);
    		}
    	}
        return new Result(true);
    }
    
    @RequestMapping(value="/saveDevice/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result saveDevice(@PathVariable("orgId") String orgId, String obj, HttpServletRequest request){
    	Org org = baseService.get(Org.class, orgId);
    	List<Device> devices = JSONObject.parseArray(obj, Device.class);
    	for(Device i: devices){
    		i.setId(UUID.randomUUID().toString());
    		i.setOrgid(orgId);
    		i.setLevelCode(org.getLevelCode());
    		i.setCreateDateTime(new Date());
    		i.setUpdateDateTime(new Date());
    		i.setVersion(0);
    		i.setDeleted(0);
    	}
    	List<Device> temps = baseService.find("from Device where orgid='"+orgId+"'");
    	baseService.batchDelete(temps);
    	baseService.batchSave(devices);
        return new Result(true);
    }
    
    @RequestMapping(value="/device_list",method = RequestMethod.GET)
    public String deviceList(){
        return "iot/scene/device_list";
    }
    
    @RequestMapping(value="/getMarkData/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result getMarkData(@PathVariable("orgId") String orgId){
    	Org org = baseService.get(Org.class, orgId);
    	List<Device> devices = baseService.find("from Device where orgid='"+orgId+"'");
    	Map<String,Object> object = new HashMap<>();
    	object.put("org", org);
    	object.put("devices", devices);
    	Result result = new Result(true);
    	result.setData(object);
    	return result;
    }
    
    @RequestMapping(value="/lookupData/{orgId}",method = RequestMethod.POST)
    @ResponseBody
    public Result lookupData(@PathVariable("orgId") String orgId){
    	List<Device> devices = baseService.find("from Device where orgid='"+orgId+"'");
    	List<Map<String,Object>> temp = new ArrayList<>();
    	for(Device i : devices){
    		if(i.getType().equals("采集器")){
    			List<SensorVo> sensors = deviceService.getMetric(i.getId());
    			StringBuffer sb = new StringBuffer();
    			if(null != sensors && sensors.size() > 0) {
    				for(SensorVo k : sensors){
    					sb.append(k.getName()+" : " + k.getValues() + "</br>");
    				}
    			}
    			Map<String,Object> object = new HashMap<>();
    			object.put("key", i.getId());
    			object.put("value", sb.toString());
    			temp.add(object);
    		}else {
    			Map<String,Object> object = new HashMap<>();
    			object.put("key", i.getId());
    			object.put("value", "设备信息:"+i.getDevip()+":"+i.getPort());
    			temp.add(object);
    		}
    	}
    	Result result = new Result(true);
    	result.setData(temp);
    	return result;
    }

}
